package com.aizhixin.test.logonAndlAnd.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aizhixin.test.common.core.PublicErrorCode;
import com.aizhixin.test.common.exception.CommonException;
import com.aizhixin.test.common.uitl.GetWeChatOpenId;
import com.aizhixin.test.common.uitl.Sample;
import com.aizhixin.test.logonAndlAnd.VO.FacePhoto;
import com.aizhixin.test.logonAndlAnd.dao.userImformationDAO;
import com.aizhixin.test.logonAndlAnd.dto.userImformationDTO;

import lombok.extern.slf4j.Slf4j;
@Service
@Transactional
@Slf4j
public class userImformationService {



	/**  
	 *<p>获取openid</p> 
	 * @Title: getWeixinOpenInfo    
	 * @version:V0.1     
	 * @param code 传入code
	 * @return    
	 * @return:String 传出openid
	 */
@Autowired
userImformationDAO userImformationDAO;
	public String getWeixinOpenInfo(String code) {
		return GetWeChatOpenId.getWeixinOpenInfo(code);
		}
	/*
	 * 获取code值并存到实体类里面，以及用户信息
	 *  调用微信自带的api转化返回openId，存到实体类里面
	 */



	/**
	 * <p>
	 * 先根据code查询是否已经注册，若注册跳转检测界面，若未注册就进行注册
	 * </p>
	 */
	public userImformationDTO findUser(String openid, userImformationDTO user) {
		user.setUserId(userImformationDAO.findIdByOpenId(openid));
		user.setToken(userImformationDAO.findtokenByOpenId(openid));
		if (null != openid) {//openid都是空的就没必要进行下面的事了
			boolean listuserservice = userImformationDAO.findUser(openid);	
			
			if (listuserservice) {// 空=flase表未注册
				int number = userImformationDAO.reUser(user);
				
				if (number > 0) {
					log.info("写入信息成功");
					return user;
				} else {
				userImformationDAO.cleanuserDTO(user);
					log.info("写入信息失败");
					return user;
				}
			} else {
				userImformationDAO.cleanuserDTO(user);
				log.info("注册过了的");
				return user;
			}
		} else {
			userImformationDAO.cleanuserDTO(user);
			log.info("openid为空");
			return user;
		}
	}



	/**
	 * <p>
	 * 人脸注册
	 * </p>
	 */
	public String faceRe(FacePhoto imageBase) {
		if (null != imageBase) {
			return Sample.faceRe(imageBase.getFace());
		}
		else {
			new CommonException(PublicErrorCode.PARAM_EXCEPTION.getIntValue(), "照片异常");
			return null;
		}
		
	}



	/**
	 * √
	 * <p>
	 * 人脸注册的时候将人脸的token写入数据库
	 * </p>
	 */
	public void addToken(userImformationDTO dto) {

		int number = userImformationDAO.updateToken(dto);
		if (number == -1) {
			new CommonException(PublicErrorCode.SAVE_EXCEPTION.getIntValue(), "更新token失败");
		}
	}





	/**  
	 *<p>人脸检测</p> 
	 * @Title: searchFace    
	 * @version:V0.1     
	 * @param imageBase
	 * @return    
	 * @return:String
	 */
	public String searchFace(String imageBase) {
	if(null != imageBase) {
	return Sample.searchFace(imageBase);
	}throw
	new CommonException(PublicErrorCode.PARAM_EXCEPTION.getIntValue(),"照片异常");
	
	}
}
















